package common.web.action;

import java.text.MessageFormat;

import org.apache.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import common.log.util.LogModule;
import common.log.util.LogType;
import common.log.vo.WebLog;
import common.rbac.po.Dictionary;
import common.util.JSONUtils;
import common.web.vo.Result;

@Controller
@RequestMapping("/system/dictionary")
public class DictionaryAction extends CommonAction {

	private static final Logger logger = Logger.getLogger(DictionaryAction.class);

	@RequestMapping("/view/{id}")
	@ResponseBody
	public Result view(@PathVariable String id) {
		Dictionary dictionary = this.baseService.get(Dictionary.class, id);
		if (null == dictionary) {
			return Result.fail("数据字典不存在");
		}
		Result result = Result.createResult(dictionary);
		return result;
	}

	@RequestMapping("/list")
	@ResponseBody
	public Result list(@RequestBody JSONObject json) {
		return super.list(json, Dictionary.class);
	}
	@RequestMapping("/listAll")
	@ResponseBody
	public Result list() {
		DetachedCriteria criteria = DetachedCriteria.forClass(Dictionary.class);
		criteria.addOrder(Order.asc("type")).addOrder(Order.asc("sort"));
		return Result.createResult(baseService.findByCriteria(criteria));
	}


	@RequestMapping("/{editType}")
	@ResponseBody
	public Result save(@PathVariable String editType, @RequestBody JSONObject json) {
		Dictionary dictionary = JSONUtils.toJavaObject(json.getJSONObject("entity"), Dictionary.class);
		if (null != dictionary) {
			WebLog log = new WebLog(LogModule.DICTIONARY, LogType.ADD, "操作失败");
			if ("update".equals(editType)) {
				log.setType(LogType.UPDATE);
			}
			try {
				// 删除关联部分
				if ("update".equals(editType)) {
					super.setCommonUpdateInfo(dictionary);
					baseService.update(dictionary);
					log.setSuccess(MessageFormat.format("修改数据字典[{0},{1}]成功!", dictionary.getType(),dictionary.getLabel()));
				} else if ("add".equals(editType)) {
					super.setCommonSaveInfo(dictionary);
					baseService.save(dictionary);
					log.setSuccess(MessageFormat.format("新增数据字典[{0},{1}]成功!", dictionary.getType(),dictionary.getLabel()));
				}
			} catch (Exception e) {
				log.setFail(MessageFormat.format("操作数据字典[{0},{1}]失败!", dictionary.getType(),dictionary.getLabel()));
				logger.error("", e);

				return Result.fail("数据错误，请刷新后重试");
			} finally {
				logService.saveLog(log);
			}
		} else {
			return Result.fail("数据错误，请刷新后重试");
		}
		return Result.SUCCESS;
	}

	@RequestMapping("/delete/{id}")
	@ResponseBody
	public Result delete(@PathVariable String id) {
		Dictionary load = baseService.load(Dictionary.class, id);
		if (null != load) {
			WebLog log = new WebLog(LogModule.DICTIONARY, LogType.DELETE, "操作失败");
			try {
				if(load.isSystem()){
					String msg = MessageFormat.format("数据字典[{0},{1}]为系统预设不能删除", load.getType(),load.getLabel());
					log.setFail(msg);
					return Result.fail(msg);
				}
				super.baseService.delete(load);
				// 日志
				log.setSuccess(MessageFormat.format("删除数据字典[{0},{1}]成功!", load.getType(),load.getLabel()));
			} catch (Exception e) {
				log.setFail(MessageFormat.format("删除数据字典[{0},{1}]失败!", load.getType(),load.getLabel()));
				logger.error("", e);

				return Result.fail("删除失败");
			} finally {
				logService.saveLog(log);
			}
		} else {
			return Result.fail("数据字典不存在");
		}
		return Result.SUCCESS;
	}
}
